#include "photograph.h"

struct kernel_list *INIT_KERNEL_LIST()			//内核链表初始化
{
	struct kernel_list *p;
	p = (struct kernel_list *)malloc(sizeof(struct kernel_list));
	if(p == NULL )
		return NULL;
	bzero(p->name,40);
	INIT_LIST_HEAD(&p->list);	
	return p;
}

void *show_photo(void * arg)
{
	FrameBuffer *buf = (FrameBuffer *)arg;
	while(1)
	{
		if(start_photo)
		{
				/* 4.获取摄像头数据 */
			pthread_mutex_lock(&PHOTO_LOCK);
			linux_v4l2_get_fream(buf);
			/* 5.显示摄像头数据在 */
			lcd_draw_jpg(	0,	0,NULL,	buf->buf,buf->length,0);
			pthread_mutex_unlock(&PHOTO_LOCK);
		}
				
	}
}

char *get_picturename(int n)
{
	char *namebuf = malloc(256);
	sprintf(namebuf,"%s%d.jpg",PICTURE_NAME,n);
	return namebuf;	
}

void find_jpg(char *path,struct kernel_list *head,char *format)			//查找一个路径下所有的format格式文件
{
	char tmp[256];
	DIR *dir;
	struct dirent *fdir;
	bzero(tmp,256);
	dir = opendir(path);
	if(dir == NULL)
	{
		perror("open dir failed!\n");
		return;
	}
	
	while((fdir = readdir(dir)) != NULL)
	{
		
		if(strstr(fdir->d_name,format) != NULL)
		{
			bzero(tmp,256);
			struct kernel_list *new = INIT_KERNEL_LIST();
			strcat(tmp,path);
			strcat(tmp,fdir->d_name);	
			strcpy(new->name,tmp);
			list_add_tail(&new->list,&head->list);
		}	
	}
}



int picture_locate(int x, int y)		//将左侧640*480分为9块，并标记为0~8，用于点击时判断是当前页面第几张
{
	int a;
	if(x >0 && x<320 && y > 0 && y<240) 
		a = 0;
	else if(x >=320 && x<640 && y > 0 && y<240)
		a = 1;
	else if(x >0 && x<320 && y >=240 && y<480)
		a = 2;
	else if(x >=320 && x<640&&y >=240 && y<480)
		a = 3;
	else if(x >650 && y > 130 && y<350)	 //拍照
		a = 4;
	else if(x >650 && y >= 350)//退出
		a = 5;
	else a = -1;	
	return a;
}





int show_photograph(void)
{
	int temp = -1;
	struct coordinate old_coor;		//触摸屏坐标
	struct coordinate new_coor;
	bool released = false;
	bool firsttouch = true;
	bool flagtime = false;
	struct kernel_list *head;
	struct kernel_list *p;
	struct list_head *q;
	
	head = INIT_KERNEL_LIST();
	find_jpg(PICTURE_NAME,head,".jpg");
	
	q = head->list.next;							//q为内核链表中头结点的小结构体的NEXT
	p = list_entry(q,struct kernel_list,list);		//p为头结点下一个大结构体
	
	int i=0,j=0;
	lcd_draw_jpg(0,0,PHOTO_BACKGROUNd,NULL,0,0);
	list_for_each_entry(p,&head->list,list)
	{	
		if(i<4)		
		lcd_draw_jpg(320*(i%2),240*(j/2),p->name,NULL,0,1);
		printf("name : %s\n",p->name );
		i++;
		j++;
	}
	q = head->list.next;
	while(1)
	{
		released = true;
		ts_trace(ts_fd, &old_coor, &released);
		if(!released)
		{
			usleep(50*1000);			
			ts_trace(ts_fd, &new_coor, &released);			
			if(!released)			
			{
				if(!firsttouch)
				{
					if (new_coor.x == old_coor.x)
					continue;
					float angle = (float)(new_coor.y-old_coor.y)
										/(new_coor.x-old_coor.x);
					if ((angle > -1 || angle < 1) && new_coor.x > old_coor.x)
					{	
						q = q->prev;
						if(list_entry(q,struct kernel_list,list) == head)
							q = q->prev;
						p = list_entry(q,struct kernel_list,list);
						lcd_draw_jpg(0,0,p->name,NULL,0,0);						
						printf("you hua!\n");
					}
					if ((angle > -1 || angle < 1) && new_coor.x < old_coor.x)
					{
						q = q->next;
						if(list_entry(q,struct kernel_list,list) == head)
							q = q->next;
						p = list_entry(q,struct kernel_list,list);
						lcd_draw_jpg(0,0,p->name,NULL,0,0);							
						printf("zuo hua!\n");						
					}	
				}
				while(1)
				{
					ts_trace(ts_fd, &new_coor, &released);
					if(released)
						break;
				}			
			}
			else
			{
				printf("x = %d, y = %d\n",old_coor.x, old_coor.y);
				if(firsttouch)
				{
					temp = picture_locate(old_coor.x, old_coor.y);
					if(temp == 4)
					{
						return 1;
					}
					else if(temp == 5)
					{
						return 2;
					}
					if(temp != -1)
					{
						for(i=0; i<temp; i++)
						{
							q = q->next;
							if(list_entry(q,struct kernel_list,list) == head)
								q = q->next;					
						}
						p = list_entry(q,struct kernel_list,list);
						lcd_draw_jpg(0,0,p->name,NULL,0,0);
					}
					firsttouch = false;					
				}
				else
				{
					temp = picture_locate(old_coor.x, old_coor.y);	
					
					if(temp == 4)
					{
						printf("1\n");
						return 1;
					}
					else if(temp == 5)
					{
						printf("2\n");
						return 2;
					}	
				}					
			}										
		}	
	}
	return 0;
}



void take_picture()
{
	int fd;
	int n;
	int number = 0;
	pthread_t pid;
	FILE *fp;
	start_photo = true;
	char *picture_name = NULL;
	FrameBuffer frame_buffer;
	struct coordinate coor;
	bool released = false;
	pthread_mutex_init(&PHOTO_LOCK,NULL);
	lcd_draw_jpg(0,0,PHOTO_BACKGROUNd,NULL,0,0);
	/*2.初始化摄像头设备 */
	linux_v4l2_device_init("/dev/video3");
	
	/*3.启动摄像头的捕捉 */
	linux_v4l2_start_capturing();
	pthread_create(&pid , NULL, show_photo, (void *)&frame_buffer);
	while(1)
	{
		released = true;
		ts_trace(ts_fd, &coor, &released);
		printf("x = %d, y = %d\n",coor.x, coor.y);
		if(coor.x >650 && coor.y > 130 && coor.y<350 && released)//拍照
		{
			if(!start_photo)
				start_photo = true;
			picture_name = get_picturename(number);	
			printf("name :%s\n",picture_name);			
			pthread_mutex_lock(&PHOTO_LOCK);
			fp = fopen(picture_name, "wb");
			if (fp == NULL) 
			{
				printf("open frame data file failed\n");
				return ;
			}
			fwrite(frame_buffer.buf, 1, frame_buffer.length, fp);
			fclose(fp);
			printf("Capture one frame saved in %s\n", picture_name);
			pthread_mutex_unlock(&PHOTO_LOCK);
			number++;
			free(picture_name);
		}	
		if(coor.x >650 && coor.y <= 130 && released)//相册
		{
			start_photo = false;
			usleep(1000);
			n = show_photograph();
			if(n == 2)
			{
				return ;
			}
			if(n == 1)
			{
				start_photo = true ;
			}
		}	
		if(coor.x >650 && coor.y >= 350 && released)//退出
		{
			pthread_cancel(pid);
			return ;
		}			
	} 
	return;	
}









